package com.ruizhong.integratedmediawithoutc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.integratedmediawithoutc.dao.TProgramMaterialMapper;
import com.ruizhong.integratedmediawithoutc.entity.TProgramMaterial;
import com.ruizhong.integratedmediawithoutc.service.TProgramMaterialService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class TProgramMaterialServiceImpl extends ServiceImpl<TProgramMaterialMapper, TProgramMaterial> implements TProgramMaterialService {
    @Override
    public void addItemInProgram(ArrayList<HashMap<String, Object>> items) {
        List<TProgramMaterial> tProgramMaterials = items.stream().map(new Function<HashMap<String, Object>, TProgramMaterial>() {
            @Override
            public TProgramMaterial apply(HashMap<String, Object> itemMap) {
                TProgramMaterial tProgramMaterial = new TProgramMaterial();
                Object duration = itemMap.get("duration");
                Object filepath = itemMap.get("filepath");
                tProgramMaterial.setDuration(duration == null?null:duration.toString());
                tProgramMaterial.setFilepath(filepath == null?null:filepath.toString());
                tProgramMaterial.setPlId((Integer) itemMap.get("pl_id"));
                tProgramMaterial.setPindex((Integer) itemMap.get("pindex"));
                Object color = itemMap.get("color");
                Object fontSize = itemMap.get("fontSize");
                tProgramMaterial.setColor(color == null?null:color.toString());
                tProgramMaterial.setFontSize(fontSize == null?null:Integer.parseInt(fontSize.toString()));
                Object oneWaySpeed = itemMap.get("oneWaySpeed");
                Object rowNum = itemMap.get("rowNum");
                tProgramMaterial.setOneWaySpeed(oneWaySpeed == null?null:Integer.parseInt(oneWaySpeed.toString()));
                tProgramMaterial.setRowNum(rowNum == null?null:Integer.parseInt(rowNum.toString()));
                return tProgramMaterial;
            }
        }).collect(Collectors.toList());
        saveBatch(tProgramMaterials);
    }

    @Override
    public List<TProgramMaterial> getMaterialIdsByProgramIds(Set<Integer> programIdsSet) {
        List<TProgramMaterial> programMaterials = lambdaQuery().in(TProgramMaterial::getPlId, programIdsSet).list();
        return programMaterials;
    }

    @Override
    public List<TProgramMaterial> getMaterialIdsByProgramId(Integer pl_id) {
        List<TProgramMaterial> list = lambdaQuery()
                .eq(TProgramMaterial::getPlId, pl_id)
                .list();
        return list;
    }

    @Override
    public void deleteByProgramId(Integer pl_id) {
        QueryWrapper<TProgramMaterial> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.lambda().eq(TProgramMaterial::getPlId,pl_id);
        remove(deleteWrapper);
    }

    @Override
    public void deleteByFileName(String file_path) {
        QueryWrapper<TProgramMaterial> tProgramMaterialQueryWrapper = new QueryWrapper<>();
        tProgramMaterialQueryWrapper.lambda().eq(TProgramMaterial::getFilepath,file_path);
        remove(tProgramMaterialQueryWrapper);
    }

    @Override
    public void deleteByProgramIds(List<Integer> programIds) {
        QueryWrapper<TProgramMaterial> tProgramMaterialQueryWrapper = new QueryWrapper<>();
        tProgramMaterialQueryWrapper
                .lambda()
                .in(CollectionUtils.isEmpty(programIds),TProgramMaterial::getPlId,programIds);
        remove(tProgramMaterialQueryWrapper);
    }
}
